<template>
  <div class="table-container">
    <vab-query-form>
      <vab-query-form-left-panel>
          <span class="table-title">权限设置</span>
      </vab-query-form-left-panel>
      <vab-query-form-right-panel>       
          <el-button icon="el-icon-plus" type="primary" @click="handleAdd">
          添加角色
        </el-button>
      </vab-query-form-right-panel>
    </vab-query-form>

    <el-table
      ref="tableSort"
      v-loading="listLoading"
      :data="list"
      :element-loading-text="elementLoadingText"
      :height="height"
      @selection-change="setSelectRows"
    >
      <el-table-column
        show-overflow-tooltip
        type="selection"
        width="55"
      ></el-table-column>
      <el-table-column show-overflow-tooltip label="序号" width="95">
        <template #default="scope">
          {{ scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip prop="name" label="角色名称">
      </el-table-column>
      <el-table-column
        show-overflow-tooltip
        label="角色描述"
        prop="description"
        class-name="table-description"
      ></el-table-column>
      <el-table-column
        show-overflow-tooltip
        label="功能权限"
      >
        <template #default="{ row }">
          <el-tag type="success" @click="handleEdit(row)">查看功能权限</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        show-overflow-tooltip
        label="报表导出权限"
      >
        <template #default="scope">
          <div v-if="scope.row.name == '超级管理员'">全部</div>
          <div v-else>
              <div v-if="scope.row.export_menu_auth">
                  <el-tag type="success" @click="handleEdit(row)">查看导出权限</el-tag>
              </div>
              <div v-else><span>无权限</span></div>
          </div>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip label="调度APP登入权限">
        <template #default="{ row }">
            <el-switch
            :value="row.app_login_status == 1 ? true : false"
            :disabled="row.name == '超级管理员' || row.name == '财务'"
            @change="changeAccess(row)">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip label="操作" class-name="table-option">
        <template #default="{ row }">
          <el-button size="mini" type="success" @click="handleEdit(row)">修改功能权限</el-button>
          <el-button size="mini" type="success" @click="handleEdit(row)">修改导出权限</el-button>
          <el-button size="mini" type="danger" @click="handleEdit(row)">删除角色</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      :background="background"
      :current-page="queryForm.pageNo"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    ></el-pagination>
  </div>
</template>

<script>
import { getRoleList } from '@/api/accountSetting'
export default {
  components: {},
  data() {
    return {
      list: [],
      listLoading: true,
      layout: 'total, sizes, prev, pager, next, jumper',
      total: 0,
      background: true,
      selectRows: '',
      elementLoadingText: '正在加载...',
      queryForm: {
        pageNo: 1,
        pageSize: 10
      },
    }
  },
  computed: {
    height() {
      return this.$baseTableHeight()
    },
  },
  created() {
    this.fetchData()
  },
  methods: {
    async fetchData() {
      this.listLoading = true
      const parms = {
        vehicle_users_gid: '59eFgUMYF9hqrRACqOBBM1v4prCwZN7K',
        page: this.queryForm.pageNo,
        list_row: this.queryForm.pageSize,
      }

      const { errCode, resData } = await getRoleList(parms)
      console.log(errCode, resData)
      this.list = resData.list
      this.total = resData.totalcount
      setTimeout(() => {
        this.listLoading = false
      }, 500)
    },
    handleAdd() {},
    handleQuery() {
      this.queryForm.pageNo = 1
      this.fetchData()
    },
    handleCurrentChange(val) {
      this.queryForm.pageNo = val
      this.fetchData()
    },
    handleSizeChange(val) {
      this.queryForm.pageSize = val
      this.fetchData()
    },
    setSelectRows(val) {
      this.selectRows = val
    },
  },
}
</script>

<style lang="scss" scoped>
.table-title {
    -webkit-box-align: center;
    -ms-flex-align: center;
    align-items: center;
    color: rgba(0,0,0,0.85);
    font-size: 16px;
    font-weight: bold;
}

::v-deep .el-table .table-description  .cell.el-tooltip {
    white-space: pre-wrap!important;
}

::v-deep .table-option .cell {
    display: flex;
    flex-wrap: wrap;
    flex-direction: column;

    button {
      margin:5px 0;
      width:120px;
    }
}


</style>